package com.example.rbacmanage.service.app.impl;

import com.example.rbacmanage.annotation.PermissionRange;
import com.example.rbacmanage.entity.app.MinAppConfig;
import com.example.rbacmanage.exception.MyException;
import com.example.rbacmanage.mapper.app.MinAppConfigMapper;
import com.example.rbacmanage.service.app.MinAppConfigService;
import com.github.pagehelper.PageInfo;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MinAppConfigServiceImpl implements MinAppConfigService {
    @Autowired
    private MinAppConfigMapper minAppConfigMapper;
    @SneakyThrows
    @Override
    public void save(MinAppConfig minAppConfig) {
        //一个店铺一种小程序类型只有一张图片
        MinAppConfig config = new MinAppConfig();
        config.setShopId(minAppConfig.getShopId());
        config.setType(minAppConfig.getType());
        Integer count = minAppConfigMapper.count(config);
        if (count !=null && count > 0) {
            throw new Exception("该店铺的该类型的配置已存在");
        }
        minAppConfig.setCreateTime(new Date());
        minAppConfigMapper.insert(minAppConfig);
    }

    @Override
    public void remove(Long[] ids) {
        for (Long id : ids) {
            minAppConfigMapper.del(id);
        }
    }

    @Override
    public void update(MinAppConfig minAppConfig) {
        minAppConfigMapper.update(minAppConfig);
    }

    @Override
    public List<MinAppConfig> getAll(MinAppConfig minAppConfig) {
        return minAppConfigMapper.list(minAppConfig);
    }

    @PermissionRange(shopAlias = "s")
    @Override
    public PageInfo<MinAppConfig> getPage(MinAppConfig minAppConfig) {
        return new PageInfo<>(getAll(minAppConfig));
    }

    @Override
    public MinAppConfig getById(Long id) {
        return minAppConfigMapper.queryOneById(id);
    }

    @Override
    public Integer getCount(MinAppConfig minAppConfig) {
        return minAppConfigMapper.count(minAppConfig);
    }

    @Override
    public MinAppConfig getOneByShopIdAndType(Long shopId, String qrCodeType) {
        return minAppConfigMapper.selectByShopIdAndType(shopId, qrCodeType);
    }
}
